<template>
  <div class="list-table">
    <div v-show="!menuKeyMark" class="area-header">
      <span class="area-header-title">{{ $t('对外付款') }}</span>
    </div>
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
    >
      <el-form-item :label="$t('申请标题')" prop="actTitle">
        <el-input v-model="queryParams.actTitle" size="small" :placeholder="$t('申请标题')" clearable @input="$forceUpdate()" @keyup.enter.native="queryHandle" />
      </el-form-item>
      <el-form-item :label="$t('主体名称')" prop="investorName">
        <el-input v-model="queryParams.investorName" size="small" :placeholder="$t('主体名称')" clearable @input="$forceUpdate()" @keyup.enter.native="queryHandle" />
      </el-form-item>
      <el-form-item :label="$t('收款方名称')" prop="payeeName">
        <el-input v-model="queryParams.payeeName" size="small" :placeholder="$t('收款方名称')" clearable @input="$forceUpdate()" @keyup.enter.native="queryHandle" />
      </el-form-item>
      <el-form-item :label="$t('申请人')" prop="userName">
        <el-input v-model="queryParams.userName" size="small" :placeholder="$t('申请人')" clearable @input="$forceUpdate()" @keyup.enter.native="queryHandle" />
      </el-form-item>
      <el-form-item v-show="menuKeyMark" :label="$t('是否已维护')" prop="whetherToMaintain">
        <dictSelect
          v-model="queryParams.whetherToMaintain"
          :clearable="true"
          dictionary="payment_maintenance"
          :placeholder="$t('请选择')"
        />
      </el-form-item>
      <el-form-item :label="$t('申请时间')">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row v-if="!menuKeyMark" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['oa:payment:edit']" type="primary" icon="el-icon-plus" size="mini" @click="addHandle">{{ $t('新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['oa:payment:export']" type="warning" icon="el-icon-download" size="mini" @click="exportHandle">{{ $t('导出') }}</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      tooltip-effect="darkTable"
      :data="oaPaymentList"
      border
      class="nowrapTable"
    >
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请标题')" align="left" prop="actTitle" :show-overflow-tooltip="true" min-width="150">
        <template slot-scope="scope">
          <el-link class="link-a" type="primary" @click="updateHandle(scope.row)">{{ scope.row.actTitle }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请人')" align="center" prop="userName" :show-overflow-tooltip="true" min-width="110" />
      <el-table-column :label="$t('申请时间')" align="center" prop="applyTime" show-overflow-tooltip min-width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('主体类型')" align="center" prop="typeName" :show-overflow-tooltip="true" min-width="110" />
      <el-table-column :label="$t('收款方名称')" align="center" prop="payeeName" :show-overflow-tooltip="true" min-width="110" />
      <el-table-column :label="$t('主体名称')" align="center" prop="investorName" :show-overflow-tooltip="true" min-width="110" />
      <el-table-column :label="$t('付款金额(元)')" align="center" prop="total" :show-overflow-tooltip="true" min-width="130">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.total) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实际付款金额(元)')" align="center" prop="total" :show-overflow-tooltip="true" min-width="130">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.actualPaymentMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('币种')" align="center" prop="currency" :formatter="currencyFormat" :show-overflow-tooltip="true" min-width="100" />
      <el-table-column fixed="right" :label="$t('审批状态')" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" fixed="right" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div v-if="!menuKeyMark">
            <ListBtn :bus-data="scope.row" :apply="scope.row.createBy.id" has-permi="oa:payment" :other-btn="!(scope.row.actStatus === 'W' && (scope.row.payCompletionTime === null || scope.row.payCompletionTime === undefined || scope.row.payCompletionTime === ''))" />
            <div v-if="scope.row.actStatus === 'W' && (scope.row.payCompletionTime === null || scope.row.payCompletionTime === undefined || scope.row.payCompletionTime === '')">
              <span slot="label">
                <el-button
                  v-hasPermi="['oa:payment:edit']"
                  size="small"
                  type="text"
                  icon="el-icon-edit"
                  @click="updatePayTimeBtn(scope.row)"
                >{{ $t('维护付款信息') }}</el-button>
                <el-tooltip v-hasPermi="['oa:payment:edit']" placement="top">
                  <div slot="content" v-html="$t('审批结束后,对费用及付款时间的梳理, </br>这里维护实际到账时间和金额.')" />
                  <i class="el-icon-question" />
                </el-tooltip>
              </span>
            </div>
          </div>
          <div v-else>
            <div v-if="scope.row.actStatus === 'W' && (scope.row.payCompletionTime === null || scope.row.payCompletionTime === undefined || scope.row.payCompletionTime === '')">
              <span slot="label">
                <el-button
                  v-hasPermi="['oa:payment:edit']"
                  size="small"
                  type="text"
                  icon="el-icon-edit"
                  @click="updatePayTimeBtn(scope.row)"
                >{{ $t('维护付款信息') }}</el-button>
                <el-tooltip v-hasPermi="['oa:payment:edit']" placement="top">
                  <div slot="content" v-html="$t('审批结束后,对费用及付款时间的梳理, </br>这里维护实际到账时间和金额.')" />
                  <i class="el-icon-question" />
                </el-tooltip>
              </span>
            </div>
            <div v-else style="display: inline-block;">
              <el-button
                v-hasPermi="['oa:payment:maintain']"
                size="small"
                type="text"
                icon="el-icon-view"
                @click="viewPayTimeBtn(scope.row)"
              >{{ $t('查看付款信息') }}</el-button>
            </div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 实际付款信息 -->
    <el-dialog
      :visible.sync="MaintainPaymentInforLoading"
      :title="$t('维护付款信息')"
      :destroy-on-close="true"
      custom-class="dialogForm"
      append-to-body
      :close-on-click-modal="false"
      :lock-scroll="false"
    >
      <MaintainPaymentInfor ref="MaintainPaymentInfor" :act-title="MaintainPaymentInforTitle" @updatePayTimeSuccess="updatePayTimeSuccess" />
    </el-dialog>
    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog v-if="formOpen" :title="$t(title)" :visible.sync="formOpen" width="70%" :close-on-click-modal="false" custom-class="dialogForm" :lock-scroll="false">
      <oaPaymentForm ref="oaPaymentForm" />
    </el-dialog>

    <!-- 维护后查看详情 -->
    <el-dialog :title="$t(title)" :visible.sync="viewOpen" width="70%" :close-on-click-modal="false" custom-class="dialogForm" :lock-scroll="false">
      <div>
        <el-table
          :data="viewDataForm.types"
          border
          class="nowrapTable formTable "
          style="overflow: auto;"
        >
          <el-table-column :label="$t('费用类型')" align="center" min-width="180px">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949;margin-right: 4px;">*</i>
              <span>{{ $t('费用类型') }}</span>
            </template>
            <template slot-scope="scope">
              <el-cascader
                v-model="scope.row.typeArray"
                filterable
                :options="getFdSubjectList"
                disabled
                style="width: 100%"
              >
                <template slot-scope="{ node, data }">
                  <span>{{ $t(data.label) }}</span>
                  <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
                </template>
              </el-cascader>
            </template>
          </el-table-column>
          <el-table-column :label="$t('实际付款时间')" align="center" min-width="160">
            <template slot-scope="scope">
              <DatePicker
                v-model="scope.row.actualPaymentTime"
                :disabled="true"
                style="width: 100%"
                :placeholder="$t('实际付款时间')"
              />
            </template>
          </el-table-column>
          <el-table-column :label="$t('实际付款金额(元)')" align="center" min-width="150">
            <template slot-scope="scope">
              <Money v-model="scope.row.actualPaymentMoney" :disabled="true" />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOaPayment,
  listOaPaymentByResult,
  delOaPayment,
  updatePayTime,
  getOaPayment
} from '@/api/oa/payment/oaPayment'
import oaPaymentForm from './oaPaymentForm.vue'
import { getFdSubjectList } from '@/api/fd/config/getTypeMain'
import ProcessStatus from '@/components/Act/ProcessStatus'
import MaintainPaymentInfor from '@/components/MaintainPaymentInfor/typeIndex'
export default {
  components: {
    oaPaymentForm,
    ProcessStatus,
    MaintainPaymentInfor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      oaPaymentList: [],
      // 弹出层标题
      title: '对外付款',
      // 日期范围
      dateRange: [],
      // 是否显示弹出层
      formOpen: false,
      // 查看维护详情页面
      viewOpen: false,
      viewDataForm: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        typeMain: undefined,
        whetherToMaintain: undefined,
        typeSub: undefined,
        payeeName: undefined
      },
      // 查询参数 付款类型
      typeArrays: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 维护付款信息遮盖层
      MaintainPaymentInforLoading: false,
      // 维护付款信息标题
      MaintainPaymentInforTitle: '',
      // 付款大类
      getFdSubjectList: [],
      // 付款币种
      currencyOptions: [],
      menuKeyMark: undefined
    }
  },
  created() {
    // 付款币种
    this.getDicts('currency').then(response => {
      this.currencyOptions = response.data
    })
    // 付款大类
    getFdSubjectList('oa_payment').then(response => {
      this.getFdSubjectList = this.changeSubjectList(response.data)
    })
    this.getList()
  },
  methods: {
    changeSubjectList(val) {
      const arr = []
      if (val.length !== 0) {
        val.forEach(item => {
          const obj = {}
          obj.value = item.value
          obj.label = this.$t(item.label)
          if (item.children && item.children.length > 0) {
            obj.children = this.changeSubjectList(item.children)
          }
          arr.push(obj)
        })
      }
      return arr
    },
    currencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.currency)
    },
    // 合计实际付款金额
    sumTotal(row) {
      var money = 0
      if (row.types !== undefined && row.types !== null && row.types !== '') {
        row.types.forEach(item => {
          if (!isNaN(parseFloat(item.actualPaymentMoney))) {
            money += parseFloat(item.actualPaymentMoney)
          }
        })
      }
      return this.moneyFormat(money, 'w') === '0' ? '' : this.moneyFormat(money, 'w')
    },
    /** 查询列表 */
    getList() {
      this.menuKeyMark = this.$route.params.menuKeyMark
      this.loading = true
      if (this.menuKeyMark) {
        listOaPaymentByResult(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.oaPaymentList = response.rows
          this.total = response.total
          this.loading = false
        })
      } else {
        listOaPayment(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.oaPaymentList = response.rows
          this.total = response.total
          this.loading = false
        })
      }
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userName: undefined,
        applyUser: undefined,
        typeMainName: undefined,
        typeSubName: undefined,
        currency: undefined,
        method: undefined,
        level: undefined,
        total: undefined,
        applyTime: undefined,
        payeeId: undefined,
        typeArray: undefined,
        depositBank: undefined,
        accountTitle: undefined,
        bankNumber: undefined,
        addressReceiver: undefined,
        payCompletionTime: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        getFdSubjectList: [],
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      if (this.typeArrays.length > 1) {
        this.queryParams.typeMain = this.typeArrays[0]
        this.queryParams.typeSub = this.typeArrays[1]
      }
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.typeArrays = []
      this.queryParams.typeMain = undefined
      this.queryParams.typeSub = undefined
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.oaPaymentForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // this.$refs.oaPaymentForm.init(null)
        this.$refs.oaPaymentForm.init(row)
        this.$refs.oaPaymentForm.$refs.form.clearValidate()
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(function() {
          return delOaPayment(row.id)
        })
        .then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
        })
        .catch(function() {})
    },
    // 查看维护付款时间详情
    viewPayTimeBtn: function(row) {
      this.viewOpen = true
      this.title = '付款信息详情'
      this.$nextTick(() => {
        getOaPayment(row.id).then(response => {
          this.viewDataForm = response.data
          if (this.viewDataForm.types === null) {
            this.viewDataForm.types = []
          }
          this.viewDataForm.types.forEach((item) => {
            this.$set(item, 'typeArray', [])
            item.typeArray.push(item.typeOne)
            item.typeArray.push(item.typeTwo)
          })
        })
      })
    },
    // 付款时间维护 : 按钮获取数据
    updatePayTimeBtn(row) {
      this.MaintainPaymentInforLoading = true
      this.MaintainPaymentInforTitle = row.actTitle + ' - ' + row.userName
      this.$nextTick(() => {
        this.$refs.MaintainPaymentInfor.init(row, 'oa_payment')
      })
    },
    // 付款时间维护 : 保存到后台
    updatePayTimeSuccess(data) {
      updatePayTime(data).then(response => {
        if (response.code === 200) {
          this.getList()
          this.MaintainPaymentInforLoading = false
        } else {
          this.msgSuccess(this.$t('维护失败'))
        }
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/payment/oaPayment/export', this.queryParams)
    }
  }
}
</script>
